package com.study.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.study.dao.entity.MovieUser;

@RestController
public class MovieUserController {
	
	@Autowired
	RestTemplate restTemplate;
	
	//直接使用ribbon的api
	@Autowired
	LoadBalancerClient LoadBalancerClient;
	
	@GetMapping("/ribbon/{id}")
	public MovieUser selectById(@PathVariable("id") int id) {
		//spring.application.name=sunxj-microservice-provider-user
		//vip=virtual ip
		MovieUser movieUser = restTemplate.getForObject("http://sunxj-microservice-provider-user/simple/" + id, MovieUser.class);
		return movieUser;
	}
	
	@GetMapping("/test")
	public String test() {
		ServiceInstance instance = LoadBalancerClient.choose("sunxj-microservice-provider-user");
		System.out.println(instance.getHost()+":"+instance.getPort());
		
		return "1";
	}

}
